package py.com.alfa.escriba.gui.security;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import py.com.alfa.escriba.gui.config.EscribaGUIApplicationContextProvider;
import py.com.alfa.escriba.gui.services.Services;
import py.com.alfa.escriba.gui.session.SystemInfo;
import py.com.alfa.escriba.gui.session.SystemSessionManager;


public class EscribaSecurityInterceptor extends HandlerInterceptorAdapter {

	private Logger logger = LoggerFactory.getLogger(getClass());
	private ApplicationContext ctx;
	
	@Override
	public boolean preHandle(HttpServletRequest request,HttpServletResponse response, Object handler) throws Exception {
//		log.info("Request =====================================");
//		Principal user = request.getUserPrincipal();
//	  	log.info("\n==============\n request User: {}\n==============\n", user);
		ctx = EscribaGUIApplicationContextProvider.getContext();
        SystemSessionManager sessionManager = (SystemSessionManager) ctx.getBean(Services.SYSTEM_SESSION_MANAGER);
        sessionManager.init(request);
        String urlFragment = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
        logger.info("REQUEST PATH: {}", urlFragment);
		SystemInfo info = (SystemInfo) ctx.getBean(Services.SYSTEM_INFO);
		info.setPagePath(urlFragment);
		return true;
	}

}
